/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.app.command.query;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Preconditions;
import com.itgrail.pilot.app.command.cmo.query.DetailOKRCmd;
import com.itgrail.pilot.app.command.co.KeyResultDetailCO;
import com.itgrail.pilot.app.command.co.ObjectiveDetailCO;
import com.itgrail.pilot.app.command.co.OkrDetailCO;
import com.itgrail.pilot.app.command.co.OkrPeriodDetailCO;
import com.itgrail.pilot.app.converter.KeyResultClientConverter;
import com.itgrail.pilot.app.converter.ObjectiveClientConverter;
import com.itgrail.pilot.app.converter.OkrPeriodClientConverter;
import com.itgrail.pilot.domain.user.contant.OkrConstants;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.ClassifyMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.KeyResultMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.ObjectiveMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.OkrPeriodMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.ClassifyDO;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.KeyResultDO;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.ObjectiveDO;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.OkrPeriodDO;
import org.springframework.beans.factory.annotation.Autowired;
import com.itgrail.grail.cqrs.annotation.CmdHandler;
import com.itgrail.grail.cqrs.command.CommandExecutorI;
import com.itgrail.grail.dto.ResultData;
import com.itgrail.grail.exception.BusinessException;

import java.util.List;


@CmdHandler
public class DetailOKRCmdExe implements CommandExecutorI<ResultData<OkrDetailCO>, DetailOKRCmd> {

    @Autowired
    private OkrPeriodMapper okrPeriodMapper;

    @Autowired
    private ObjectiveMapper mapper;

    @Autowired
    private KeyResultMapper keyResultMapper;

    @Autowired
    private ObjectiveClientConverter objectiveClientConverter;

    @Autowired
    private KeyResultClientConverter keyResultClientConverter;

    @Autowired
    private OkrPeriodClientConverter okrPeriodClientConverter;

    @Autowired
    private ClassifyMapper classifyMapper;

    @Override
    public ResultData execute(DetailOKRCmd cmd) {
        ResultData resultData = ResultData.success(null);
        check(cmd);
        resultData.setData(findDetail(cmd.getId()));
        return resultData;
    }

    protected boolean check(DetailOKRCmd cmd) {
        Preconditions.checkNotNull(cmd, "cmd不能为null");
        Preconditions.checkNotNull(cmd.getId(), "id不能为null");
        return true;
    }

    /**
     * 封装okr详情
     *
     * @param objectiveId
     * @return
     */
    protected OkrDetailCO findDetail(Integer objectiveId) {
        OkrDetailCO okrDetailCO = new OkrDetailCO();
        //获取目标信息
        ObjectiveDO objectiveDO = mapper.selectById(objectiveId);
        Integer classifyId = objectiveDO.getClassifyId();
        ClassifyDO classifyDO = classifyMapper.selectById(classifyId);
        ObjectiveDetailCO objectiveDetailCO = objectiveClientConverter.dataToClient(objectiveDO);
        if (classifyDO != null) {
            objectiveDetailCO.setClassifyName(classifyDO.getName());
        }
        okrDetailCO.setObjectiveDetailCO(objectiveDetailCO);
        if (objectiveDO == null) {
            throw new BusinessException("500", "没有查询到OKR内容");
        }
        //获取考核周期实体
        OkrPeriodDO okrPeriodDO = okrPeriodMapper.selectById(objectiveDO.getOkrPeriodId());
        OkrPeriodDetailCO okrPeriodDetailCO = okrPeriodClientConverter.dataToClient(okrPeriodDO);
        okrDetailCO.setOkrPeriodDetailCO(okrPeriodDetailCO);

        //获取关键成果
        QueryWrapper<KeyResultDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", OkrConstants.NOT_DELETED);
        queryWrapper.eq("objective_id", objectiveId);
        List<KeyResultDO> keyResultDOS = keyResultMapper.selectList(queryWrapper);
        List<KeyResultDetailCO> keyResultDetailCOS = keyResultClientConverter.dataToClients(keyResultDOS);
        okrDetailCO.setKeyResultDetailCO(keyResultDetailCOS);
        return okrDetailCO;
    }

}
